#include "head.h"
#define COMMADNNUM 8
int Send_Command(int sockfd,const Command *command){
    int ret = send(sockfd,command,sizeof(command),0);
    return ret;
} 

//传入两个参数，一个是命令行，一个是命令对象
int Which_Command(char* Combuf,Command *command){    
    char* CMD=strtok(Combuf," ");
    char* Path=strtok(NULL," ");
    char CommandList[COMMADNNUM][10]={"cd","ls","puts","gets","remove","pwd","mkdir","rmdir"};
    int seq=-1;//命令的序号
    for(int i=0;i<COMMADNNUM;++i){
        if(strcmp(CMD,CommandList[i])==0){
            seq=i;
            break;
        }
    }
    ERROR_CHECK(seq,-1,"错误的命令！");
    switch(seq){
    case CD:
        command->Name=CD;
        strcpy(command->object,Path);
        break;
    case LS:
        command->Name=LS;
        strcpy(command->object,Path);
        break;
    case PUTS:
        command->Name=PUTS;
        strcpy(command->object,Path);
        break;
    case GETS:
        command->Name=GETS;
        strcpy(command->object,Path);
        break;
    case REMOVE:
        command->Name=REMOVE;
        strcpy(command->object,Path);
        break;
    case PWD:
        command->Name=PWD;
        strcpy(command->object,Path);
        break;
    case MKDIR:
        command->Name=MKDIR;
        strcpy(command->object,Path);
        break;
    case RMDIR:
        command->Name=RMDIR;
        strcpy(command->object,Path);
        break;
    }
    return 0;
}

int recvAtrain(int sockfd){
    train_t train;
    bzero(&train,sizeof(train));
    recv(sockfd,&train.length,sizeof(int),0);
    recv(sockfd,&train.data,train.length,0);
    printf("%s\n",train.data);
    return 0;
}

int Do_Command(int sockfd,Command* command){
    int length;
    switch(command->Name){
    case CD:
        recvAtrain(sockfd);
        break;
    case LS:
        recvAtrain(sockfd);
        break;
    case GETS:
        recvFile(sockfd,command->object);
        break;
    case PUTS:
        uploadFile(sockfd,command->object);
        break;
    case PWD:
        recvAtrain(sockfd);
        break;
    case MKDIR:
        break;
    case RMDIR:
        break;
    default:
        break;
    }
}
